using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using iiuga.Web.UI;
using Portal.API;


namespace Portal.Modules.AdminPortal
{
	/// <summary>
	///		Summary description for AdminPortal.
	/// </summary>
	public partial class AdminPortalControl : AdminPortal
	{
		private string currentRef = "";
		
		private string currentParentRef = "";

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				tabEdit.Visible = false;
				BuildTree();
				SelectTab("");
			}
		}
		
		protected void OnSave(object sender, PortalDefinition.Tab t)
		{
			BuildTree();
		}
		
		protected void OnCancel(object sender, PortalDefinition.Tab t)
		{
		}
		
		protected void OnDelete(object sender, PortalDefinition.Tab t)
		{
			BuildTree();
			SelectTab(currentParentRef);
		}

		protected override void LoadViewState(object bag)
		{
			base.LoadViewState(bag);
			currentRef = (string)ViewState["CurrentReference"];
			currentParentRef = (string)ViewState["CurrentParentReference"];
		}
		
		protected override object SaveViewState()
		{
			ViewState["CurrentReference"] = currentRef;
			ViewState["CurrentParentReference"] = currentParentRef;
			return base.SaveViewState();
		}

		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="tabs">IList - can be a Array or ArrayList</param>
		/// <param name="parent"></param>
		
		override public void BuildTree()
		{
			PortalDefinition pdB = PortalDefinition.Load();
			tree.Elements[0].Elements.Clear();
			BuildTree(pdB.tabs, tree.Elements[0]);
			tree.Elements[0].Expand();
			tree.Elements[0].Text = "<a class=\"rootPortal\" href=" + Page.GetPostBackClientHyperlink(this, AppConfig.PORTAL_BROWSE) + ">Browse Portal</a>";

			PortalDefinition pdA = PortalDefinition.Load(AppConfig.PORTAL_ADMIN);
			treeAdmin.Elements[0].Elements.Clear();
			BuildTree(pdA.tabs, treeAdmin.Elements[0]);
			treeAdmin.Elements[0].Expand();
			treeAdmin.Elements[0].Text = "<a class=\"rootPortal\" href=" + Page.GetPostBackClientHyperlink(this, AppConfig.PORTAL_ADMIN) + ">Administrator Portal</a>";
		}

		private void BuildTree(ArrayList tabs, TreeElement parent)
		{
			foreach (PortalDefinition.Tab t in tabs)
			{
				int n = parent.Elements.Add(t.title);
				parent.Elements[n].Key = t.reference;
				parent.Elements[n].Text = "<a class=\"linkItem\" href=" + Page.GetPostBackClientHyperlink(this, t.reference) + ">" + t.title + "</a>";

				if (t.tabs != null && t.tabs.Count != 0)
				{
					BuildTree(t.tabs, parent.Elements[n]);
					parent.Elements[n].Expand();
				}
				else
				{
					parent.Elements[n].ImageIndex = 0;
				}
			}
		}
		
		override public void SelectTab(string strTabRef)
		{
			PortalDefinition pd  = PortalDefinition.Load(Helper.GetPortalConfig(strTabRef.ToLower()));
			currentRef = strTabRef;
			if (strTabRef.Equals(AppConfig.PORTAL_BROWSE) || strTabRef.Equals(AppConfig.PORTAL_ADMIN)) 
			{
				currentParentRef = "";
				tabEdit.Visible = false;
				tabList.LoadData(pd, strTabRef);
			}
			else
			{
				PortalDefinition.Tab t = pd.GetTab(strTabRef);
				currentParentRef = t.parent != null ? t.parent.reference : "";
				tabList.LoadData(t, Helper.GetPortalConfig(strTabRef.ToLower()));
				tabEdit.Visible = true;
				tabEdit.LoadData(t);
			}
		}

		override public void AddTab()
		{
			PortalDefinition pd = PortalDefinition.Load();
			PortalDefinition.Tab t = PortalDefinition.Tab.Create();

			if (currentRef == "") 
			{
				pd.tabs.Add(t);
			}
			else
			{
				PortalDefinition.Tab pt = pd.GetTab(currentRef);
				pt.tabs.Add(t);
			}

			pd.Save();

			BuildTree();
			SelectTab(t.reference);
		}

		override public void AddTab(string portalId)
		{
			PortalDefinition pd = PortalDefinition.Load(portalId);
			PortalDefinition.Tab t = PortalDefinition.Tab.Create(portalId);

			if (currentRef.Equals(AppConfig.PORTAL_BROWSE) || currentRef.Equals(AppConfig.PORTAL_ADMIN))
			{
				pd.tabs.Add(t);
			}
			else
			{
				PortalDefinition.Tab pt = pd.GetTab(currentRef);
				pt.tabs.Add(t);
			}

			pd.Save(portalId);

			BuildTree();
			SelectTab(t.reference);
		}
		override public void MoveTabUp(int index)
		{
			if (index <= 0) return;

			PortalDefinition pd = PortalDefinition.Load();
			ArrayList a = null;
			if (currentRef == "")
			{
				// Root
				a = pd.tabs;
			}
			else
			{
				PortalDefinition.Tab pt = pd.GetTab(currentRef);
				a = pt.tabs;
			}

			PortalDefinition.Tab t = (PortalDefinition.Tab)a[index];
			a.RemoveAt(index);
			a.Insert(index - 1, t);

			pd.Save();

			// Rebind
			BuildTree();
			SelectTab(currentRef);
		}

		override public void MoveTabDown(int index)
		{
			PortalDefinition pd = PortalDefinition.Load();
			ArrayList a = null;
			if (currentRef == "")
			{
				// Root
				a = pd.tabs;
			}
			else
			{
				PortalDefinition.Tab pt = pd.GetTab(currentRef);
				a = pt.tabs;
			}

			if (index >= a.Count - 1) return;

			PortalDefinition.Tab t = (PortalDefinition.Tab)a[index];
			a.RemoveAt(index);
			a.Insert(index + 1, t);

			pd.Save();

			// Rebind
			BuildTree();
			SelectTab(currentRef);
		}

		override public void RaisePostBackEvent(string eventArgument)
		{
			SelectTab(eventArgument);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
